#define DEBUG
#include <cstdio>

using namespace std;

const int maxn=1000, inf=0x7ffffff;

int read() {
  char c=getchar();
  for (; c<'0' || c>'9'; c=getchar());
  int ret=0;
  for (; c>='0' && c<='9'; c=getchar()) {
    ret = ret*10+c-'0';
  }
  return ret;
}

int min(int a, int b) {
  return a<b ? a : b;
}

int getFa(int *set, int o) {
  return set[o]==o ? o : (set[o]=getFa(set, set[o]));
}

void sort(int a[][3], int l, int r) {
  int i=l, j=r, mid=a[(l+r)/2][0], temp;
  for (; i<j; ) {
    for (; a[j][0]>mid; j--);
    for (; a[i][0]<mid; i++);
    if (i<=j) {
      for (int k=0; k<3; k++) {
	temp=a[i][k], a[i][k]=a[j][k], a[j][k]=temp;
      }
      i++, j--;
    }
  }
  if (i<r) {
    sort(a, i, r);
  }
  if (j>l) {
    sort(a, l, j);
  }
}

int main() {
#ifdef DEBUG
  freopen("1.in", "r", stdin);
  freopen("1.out", "w", stdout);
#endif

  static int side[maxn*maxn+maxn+1][3], set[maxn+1];
  int n=read();
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=n; j++) {
      side[i*n+j-n][0]=read(), side[i*n+j-n][1]=i, side[i*n+j-n][2]=j;
    }
  }
  for (int i=1; i<=n; i++) {
    int v;
    scanf("%d", &v);
    side[n*n+i][0]=v;
    side[n*n+i][1]=0, side[n*n+i][2]=i;
    set[i]=i;
  }
  sort(side, 1, n*n+n);
  
  int ans=0;
  for (int i=1, x, y; i<=n*n+n; i++) {
    if ((x=getFa(set, side[i][1]))!=(y=getFa(set, side[i][2]))) {
      set[y] = x;
      ans += side[i][0];
    }
  }

  printf("%d", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
